<template>
  <div @click="checkRadio" class="radio">
    <span
      class="radio_arrow"
      :class="{
        radio_arrow_check: radioValue === value,
        radio_arrow_check_disabled: radio_disabled,
      }"
    ></span>
    <span class="radio_label" :title="slotText"><slot>option</slot></span>
  </div>
</template>

<script setup>
import { ref, inject, useSlots } from "vue";
const props = defineProps({
  value: {
    type: [String, Number, Boolean],
    default: "",
  },
});
const { radioValue, updateRadioValue, radio_disabled } = inject("radioValue");
const cursorStyle = radio_disabled.value
  ? "var(--cursor_disabled)"
  : "var(--cursor_pointer)";
const checkRadio = () => {
  if (radio_disabled.value) return;
  updateRadioValue(props.value);
};
// 设置过长文本提示词
const slots = useSlots();
const slot = slots.default ? slots.default() : undefined;
const slotText = ref("");
if (slot) {
  slotText.value = slot[0].children;
} else {
  slotText.value = "option";
}
</script>
<style scoped>
@keyframes blink {
  0% {
    opacity: 1;
  }

  50% {
    opacity: 0;
  }
}

.radio {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  cursor: v-bind(cursorStyle);
}
.radio_arrow {
  width: 16px;
  height: 16px;
  position: relative;
}

.radio_label {
  max-width: 150px;
  white-space: nowrap; /* 确保文本在一行内显示 */
  overflow: hidden; /* 隐藏溢出的内容 */
  text-overflow: ellipsis; /* 使用省略号表示文本溢出 */
}
.radio_arrow_check::before {
  position: absolute;
  top: 0;
  left: 0;
  transform: translateY(-50%);
  content: "";
  width: 2px;
  height: 2px;
  color: #212529;
  box-shadow: 2px 2px, 4px 2px, 2px 4px, 4px 4px, 6px 4px, 8px 4px, 2px 6px,
    4px 6px, 6px 6px, 8px 6px, 10px 6px, 2px 8px, 4px 8px, 6px 8px, 8px 8px,
    10px 8px, 12px 8px, 2px 10px, 4px 10px, 6px 10px, 8px 10px, 10px 10px,
    2px 12px, 4px 12px, 6px 12px, 8px 12px, 2px 14px, 4px 14px;
  animation: blink 1s infinite steps(1);
}
.radio_arrow_check_disabled::before {
  animation: none;
}
</style>
